package com.example.demo.controller;


import com.example.demo.entity.Category;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    @SuppressWarnings("all")//百度的
            CategoryService categoryService;


    @RequestMapping("/list")
    public String list11(Model model, @RequestParam(value="page", defaultValue="0")Integer page,
                         @RequestParam(value="size", defaultValue="5")Integer size) {
        if (page==null || page<=0) { page = 1; } // 将第0页转换为习惯的第1页
        if (size==null || size<=0) { size = 5; }
        Sort sort = Sort.by(Sort.Direction.ASC, "id"); // 排序字段是实体类的属性
        Pageable pageable = PageRequest.of(page - 1, size, sort); // （当前页，每页记录数，排序方式）
        Page<Category> bookPage =categoryService.getCategoryList(pageable);
        model.addAttribute("bookPage", bookPage);
        model.addAttribute("size", size);


        return "c/list";
    }

//    @RequestMapping("/list")
//    public String c_search(Model model){
//        List<Category> categories=categoryService.findAll();
//        model.addAttribute("categories",categories);
//        return "c/list" ;
//    }


    @RequestMapping("/edit/{id}")
    public String c_edit(Model model, @PathVariable Long id) {
        Category category=categoryService.getCategoryById(id);
        model.addAttribute("category",category);
        return "c/edit";//打开创建图书修改界面
    }

    @PostMapping("/edit")
    public String c_modify(@RequestParam Long id, @RequestParam String name){
        Category c=categoryService.getCategoryById(id);
       c.setName(name);
       categoryService.modify(c);
        return "redirect:/category/list";
    }

    @RequestMapping("/add")
    public  String c_add(){
        return "c/add";//打开创建图书界面
    }


    @PostMapping("/add2")
    public  String c_add2(@RequestParam Long id, @RequestParam String name){

        categoryService.addCategory(id,name);
        return "redirect:/category/list";
    }


    @RequestMapping("/delete/{id}")
    public String c_delete(Model model, @PathVariable(value = "id") Long id)
    {
        Category category=categoryService.getCategoryById(id);
        model.addAttribute("category",category);
                return "c/delete";
    }

    @PostMapping("/delete")
    public  String c_delete2(@RequestParam Long id){

        categoryService.delete2(id);
        return "redirect:/category/list";
    }


    @RequestMapping("/findid")
    public String c_findid(Model model, @RequestParam(value = "catid") Long id){
        List<Category> categoryList=categoryService.findById(id);
        model.addAttribute("list",categoryList);
        return "c/show";
    }


    @RequestMapping("/findname")
    public String c_findname(Model model, @RequestParam(value = "catname") String name){
        List<Category> categoryList=categoryService.findByName(name);
        model.addAttribute("list",categoryList);
        return "c/show";
    }















    @GetMapping("/get/{id}")
    public Category get(@PathVariable Long id){
       Category category=categoryService.getCategoryById(id);
        return category;
    }

    @GetMapping("/search/{name}")
    public List<Category> search(@PathVariable String name){
        return categoryService.findByName(name);
    }

    @PostMapping("/modify")
    public boolean modify( Long cat_id, String title){
        Category category=new Category();
        category.setId(cat_id);
        category.setName(title);
        return categoryService.modify(category);
    }
    @PostMapping("/delete/{title}")
    public boolean delete(@PathVariable String title){
        return categoryService.delete(title);
    }

}
